package com.example.fang.demodruid.concurrency7;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.IntStream;

public class MyTest6 {

    public static void main(String[] args) throws Exception {

        ExecutorService executorService=new ThreadPoolExecutor(4,10,10,
                TimeUnit.SECONDS,new LinkedBlockingDeque<>(20),new ThreadPoolExecutor.AbortPolicy());
        CompletionService<Integer> completionService=new ExecutorCompletionService<>(executorService);
        //10个任务随机执行
        IntStream.range(0,10).forEach(i->{
            completionService.submit(()->{
                //Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName());
                return i;
            });
        });
        List<Integer> list=new ArrayList<>();
        for(int i=0;i<10;i++){
            //获取任务的执行结果
            int result=completionService.take().get();
            list.add(result);
           // System.out.println(result);
        };
        System.out.println("所有线程执行完毕"+list);
        executorService.shutdown();
    }
}
